<?php

Raise::load('core.RaiseTimeSpan');

/**
 * TestCoreRaiseTimeSpan class
 * Testing unit of 'phpraise/core/RaiseTimeSpan.php'
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseTimeSpan extends RaiseTest {
    
    public static function testClass(){
        self::assertClassExists('RaiseTimeSpan');
        self::assertClassExtends('RaiseTimeSpan', 'RaiseDataType');
    }

    public static function testTime(){
        $ts = new RaiseTimeSpan();
        self::assertObjectClass($ts, 'RaiseTimeSpan');
        self::assertStrictEqual($ts->totalDays(), 0.0);
        self::assertStrictEqual($ts->totalHours(), 0.0);
        self::assertStrictEqual($ts->totalMinutes(), 0.0);
        self::assertStrictEqual($ts->totalSeconds(), 0.0);

        self::assertStrictEqual($ts->day(), 0.0);
        self::assertStrictEqual($ts->hour(), 0.0);
        self::assertStrictEqual($ts->minute(), 0.0);
        self::assertStrictEqual($ts->second(), 0.0);
    }

    public static function testTime2(){
        $ts = new RaiseTimeSpan(72);
        self::assertObjectClass($ts, 'RaiseTimeSpan');
        self::assertTrue($ts->totalDays() > 0);
        self::assertStrictEqual($ts->totalHours(), 0.02);
        self::assertStrictEqual($ts->totalMinutes(), 1.2);
        self::assertStrictEqual($ts->totalSeconds(), 72.0);

        self::assertStrictEqual($ts->day(), 0.0);
        self::assertStrictEqual($ts->hour(), 0.0);
        self::assertStrictEqual($ts->minute(), 1.0);
        self::assertStrictEqual($ts->second(), 12.0);
    }

    public static function testCreateNegativeTime(){
        try{
            $ts = new RaiseTimeSpan(-72);
            self::addError('Excepting Exception to be raised');
        }catch(Exception $ex){
            self::assert($ex);
        }
    }

    public static function testTimeAdd(){
        $ts1 = new RaiseTimeSpan(305);
        $ts2 = new RaiseTimeSpan(3624);

        $ts = $ts1->add($ts2);
        self::assertNotEqual($ts, $ts1);
        self::assertNotEqual($ts->hashCode(), $ts1->hashCode());
        self::assertObjectClass($ts, 'RaiseTimeSpan');
        self::assertStrictEqual($ts->totalSeconds(), 3929.0);
        $ts = $ts2->add($ts1);
        self::assertObjectClass($ts, 'RaiseTimeSpan');
        self::assertStrictEqual($ts->totalSeconds(), 3929.0);
    }

    public static function testTimeSubtract(){
        $ts1 = new RaiseTimeSpan(305);
        $ts2 = new RaiseTimeSpan(3624);

        $ts = $ts2->subtract($ts1);
        self::assertNotEqual($ts, $ts2);
        self::assertNotEqual($ts->hashCode(), $ts2->hashCode());
        self::assertObjectClass($ts, 'RaiseTimeSpan');
        self::assertStrictEqual($ts->totalSeconds(), 3319.0);
        try{
            $ts = $ts1->subtract($ts2);
            self::addError('Excepting Exception to be raised');
        }catch(Exception $ex){
            self::assert($ex);
        }
    }

}
